package edu.gym4all.data.sqlite.android.init;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteDBInit extends SQLiteOpenHelper {

	public static final String TB_EXERCISE = "tb_exercise";
	public static final String TB_TRAINING = "tb_training";
	public static final String TB_ASSOC_EXERCISE_TRAINING = "tb_exercise_training";
	public static final String TB_TRAINING_PLAN = "tb_training_plan";

	public static final String COD_EXERCISE = "cod_exercise";
	public static final String COD_TRAINING = "cod_training";
	public static final String COD_TRAINING_PLAN = "cod_training_plan";
	public static final String COD_TEX = "cod_exercicio_training";

	public static final String EXERCISE_NOME = "name";
	private static final String EXERCISE_REPS = "repetitions";
	private static final String EXERCISE_SERIES = "series";
	private static final String EXERCISE_TIME = "time";
	private static final String MUSCLE_TYPE = "muscle_type";
	private static final String EXERCISE_TYPE = "exercise_type";
	public static final String TRAINING_NAME = "training_name";

	private static final String DATABASE_NAME = "gym4all";
	private static final int DATABASE_VERSION = 1;

	private static final String CREATE_TRAINING_PLAN_TB = "create table "
			+ TB_TRAINING_PLAN + "( " + COD_TRAINING_PLAN
			+ " integer primary key autoincrement);";

	private static final String CREATE_EXERCISE_TB = "create table "
			+ TB_EXERCISE + "( " + COD_EXERCISE
			+ " integer primary key autoincrement, " + EXERCISE_NOME
			+ " text not null," + EXERCISE_REPS + ", " + EXERCISE_SERIES + ", "
			+ EXERCISE_TIME + ", " + MUSCLE_TYPE + ", " + EXERCISE_TYPE + ");";

	private static final String CREATE_TRAINING_TB = "create table "
			+ TB_TRAINING + "( " + COD_TRAINING
			+ " integer primary key autoincrement, " + TRAINING_NAME
			+ " text not null, " + COD_TRAINING_PLAN + ", FOREIGN KEY ("
			+ COD_TRAINING_PLAN + ") REFERENCES " + TB_TRAINING_PLAN + "("
			+ COD_TRAINING_PLAN + "));";

	private static final String CREATE_ASSOC_EXERCISE_TRAINING_TB = "create table "
			+ TB_ASSOC_EXERCISE_TRAINING
			+ "( "
			+ COD_TEX
			+ " integer primary key autoincrement, "
			+ COD_EXERCISE
			+ ", "
			+ COD_TRAINING
			+ ", FOREIGN KEY ("
			+ COD_EXERCISE
			+ ") REFERENCES "
			+ TB_EXERCISE
			+ "("
			+ COD_EXERCISE
			+ "),  FOREIGN KEY ("
			+ COD_TRAINING
			+ ") REFERENCES "
			+ TB_TRAINING
			+ " ("
			+ COD_TRAINING + "));";

	private final Context context;
	private SQLiteDatabase db;

	public SQLiteDBInit(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.db = this.getWritableDatabase();
		// context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE,
		// null);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.beginTransaction();
		try {
			db.execSQL(CREATE_TRAINING_PLAN_TB);
			db.execSQL(CREATE_TRAINING_TB);
			db.execSQL(CREATE_EXERCISE_TB);
			db.execSQL(CREATE_ASSOC_EXERCISE_TRAINING_TB);
			db.setTransactionSuccessful();
		} catch (SQLException e) {
			Log.e("Erro ao criar as tabelas e testar os dados", e.toString());
		} finally {
			db.endTransaction();
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}
